package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwj.bo.SelectWorkSubmitInfoBo;
import com.zwj.bo.WorkSubmitBo;
import com.zwj.bo.WorkSubmitInfo;

import com.zwj.entity.UserEntity;
import com.zwj.entity.WorkSubmitInfoEntity;
import com.zwj.mapper.StudentTaskMapper;
import com.zwj.mapper.TeacherOperationMapper;
import com.zwj.mapper.WorkSubmitInfoMapper;
import com.zwj.service.ISysUserService;
import com.zwj.service.IWorkSubmitInfoService;
import com.zwj.service.WorkSubmitInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.TokenUtils;
import com.zwj.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作业提交信息表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-07-21
 */
@Service
public class WorkSubmitInfoServiceImpl implements WorkSubmitInfoService {

    @Autowired
    HttpServletRequest request;
    @Autowired
    ISysUserService iSysUserService;
    @Autowired
    IWorkSubmitInfoService iWorkSubmitInfoService;
    @Autowired
    TeacherOperationMapper mapper;
    @Autowired
    StudentTaskMapper studentTaskMapper;;
    /*查询作业*/
    @Override
    public Result<SelectWorkSubmitInfoVo> jobManagement(SelectWorkSubmitInfoBo selectWorkSubmitInfoBo) {
        String workStatus=null;
        String types=null;
        String typess=null;
        String sss=null;
        String studentId= TokenUtils.getMemberIdByJwtToken(request);//获取学生id
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",studentId);
        String  studentName=iSysUserService.getOne(queryWrapper).getName();
        if (selectWorkSubmitInfoBo.getType()==null||selectWorkSubmitInfoBo.getType()=="" ||selectWorkSubmitInfoBo.getType().equals("待提交")){
            sss="待提交";
            workStatus="0";

        }else if (selectWorkSubmitInfoBo.getType().equals("待批复")){
            sss="待批复";
            typess="待批复";
            workStatus="1";
        }else {
            sss="已批复";
            workStatus="1";
            types="已批复";
        }
        if (selectWorkSubmitInfoBo.getCurrent()==null || selectWorkSubmitInfoBo.getCurrent()<=0){
            selectWorkSubmitInfoBo.setCurrent(1);
        }
        if (selectWorkSubmitInfoBo.getCurrent()==null || selectWorkSubmitInfoBo.getSize()==null || selectWorkSubmitInfoBo.getSize()<=0){
            selectWorkSubmitInfoBo.setSize(10);
        }
        Page page = PageHelper.startPage(selectWorkSubmitInfoBo.getCurrent(),selectWorkSubmitInfoBo.getSize(),"id  DESC");
        List<SelectWorkSubmitInfoVo> list=studentTaskMapper.jobManagement(selectWorkSubmitInfoBo,studentName,workStatus,typess,types);
//        List<SelectWorkSubmitInfoVo> list=studentTaskMapper.jobManagement();
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setPages(page.getPages());//总页数
        pageInfo.setTotal(page.getTotal());//总条数
        for (int i=0;i<list.size();i++){
            if(list.get(i).getWorkType().equals("0")){
                list.get(i).setWorkType("平时作业");
            }else if(list.get(i).getWorkType().equals("1")){
                list.get(i).setWorkType("阶段性作业");
            }else {
                list.get(i).setWorkType("最终交互作业");
            }
            list.get(i).setWorkStatus(sss);

        }
        Map<String,Object> map = new HashMap<>();
        map.put("page",page.getPages());
        map.put("total",page.getTotal());
        map.put("info",list);
        return Result.success(map);
    }


    /*作业任务*/
    @Override
    public Result<SelectWorkInfoVo> StudentTask(Integer id) {
        SelectWorkInfoVo restPerTask=studentTaskMapper.StudentTask(id);
        //获取作业id
        QueryWrapper<WorkSubmitInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long  work_id=iWorkSubmitInfoService.getOne(queryWrapper).getWorkId();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(work_id,"0");
        if (list!=null && list.size()>0){
            restPerTask.setRestEnclosures(list);
        }
        return Result.success().data(restPerTask);
    }

    /*查看老师批复详情*/
    @Override
    public Result StudentViewApproval(Integer id) {
        QueryWrapper<WorkSubmitInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  teacherRemark=iWorkSubmitInfoService.getOne(queryWrapper).getTeacherRemark();
        BigDecimal score=  iWorkSubmitInfoService.getOne(queryWrapper).getScore();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(id.longValue(),"3");
        Map<String,Object> map = new HashMap<>();
        map.put("teacherRemark",teacherRemark);
        map.put("score",score);
        map.put("info",list);
        return Result.success(map);
    }


    /*提交学生答题*/
    @Override
    public Result StudentAnswers(Integer id, String answerRemark) {
        String is_draft="0";
        String workStatus="1";
        boolean b=studentTaskMapper.StudentAnswers(id,answerRemark,is_draft,workStatus);
        if (b==false){
            return Result.error("请重新提交");
        }
        return Result.success("提交成功");
    }

    /*保存草稿*/
    @Override
    public Result SaveDraft(Integer id, String answerRemark) {
        QueryWrapper<WorkSubmitInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  workStatus=iWorkSubmitInfoService.getOne(queryWrapper).getWorkStatus();
        if (workStatus.equals("1")){
            return Result.error("你已提交不能保存草稿");
        }
        String is_draft="1";
        boolean b=studentTaskMapper.StudentAnswers(id,answerRemark,is_draft,workStatus);
        if (b==false){
            return Result.error("请重新提交");
        }
        return Result.success("提交成功");
    }
    /*学生答题*/
    @Override
    public Result ViewAnswers(Integer id) {
        QueryWrapper<WorkSubmitInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  answerRemark=iWorkSubmitInfoService.getOne(queryWrapper).getAnswerRemark();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(id.longValue(),"1");
        Map<String,Object> map = new HashMap<>();
        map.put("answerRemark",answerRemark);
        map.put("info",list);
        return Result.success(map);
    }


//    @Override
//    public Result showSubmitWorkInfo(SelectWorkSubmitInfoBo selectWorkSubmitInfoBo, String id) {
//        //数据校验
////        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getCreateTime(),"创建时间不能为空");
////        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getTitle(),"作业主题不能为空");
////        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getCourseProjectInfoId(),"项目组名称不能为空");
////        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getWorkType(),"作业类型不能为空");
////        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getCreateTime(),"老师不能为空");
////        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getCompleteDate(),"计划完成时间不能为空");
//
//        int number = 1;
//        int page = 1;
//        if (id == null) {
//            number = selectWorkSubmitInfoBo.getNumber();
//            page = selectWorkSubmitInfoBo.getPage();
//            //分页参数是否正确
//            DataCheckUtils.isValid(PaginationUtils.isPagingParameters(number, page), "分页参数错误!");
//        }
//        //查询作业 mybatis
//        List<Object> result = workSubmitInfoMapper.selectWorkInfo(new SelectWorkSubmitInfoQuery(PaginationUtils.getPageIndex(page, number), number, id,
//                selectWorkSubmitInfoBo.getTitle(), selectWorkSubmitInfoBo.getCompleteDate(), selectWorkSubmitInfoBo.getCourseProjectInfoId(),
//                selectWorkSubmitInfoBo.getWorkType(), selectWorkSubmitInfoBo.getAppendsLink(), selectWorkSubmitInfoBo.getRemark(), selectWorkSubmitInfoBo.getDelFlag(),
//                selectWorkSubmitInfoBo.getCreateBy(), selectWorkSubmitInfoBo.getCreateTime(), selectWorkSubmitInfoBo.getUpdateBy(),
//                selectWorkSubmitInfoBo.getUpdateTime()));
//        if (id != null) {
//            int sum = ((List<Integer>) result.get(1)).get(0);
//            if (sum == 0) {
//                return Result.error("作业不存在");
//            }
//            return Result.success(((List<SelectWorkSubmitInfoVo>) result.get(0)).get(0));
//        }
//        int sum = ((List<Integer>) result.get(1)).get(0);
//        List<SelectWorkSubmitInfoVo> selectWorkSubmitInfoVos = (List<SelectWorkSubmitInfoVo>) result.get(0);
//        PaginationDate paginationDate = PaginationUtils.getPaginationDate(number, page, sum);
//        //记录总数为0
//        if (sum == 0) {
//            SelectWorkSubmitInfoVo selectWorkSubmitInfoVo = new SelectWorkSubmitInfoVo();
//            selectWorkSubmitInfoVos.add(selectWorkSubmitInfoVo);
//            return Result.success(new QueryReturnDate(paginationDate, selectWorkSubmitInfoVos));
//        }
//        //当前页大于总页数
//        if (PaginationUtils.isValid(paginationDate)) {
//            return Result.error("当前页大于总页数");
//        }
//        return Result.success(new QueryReturnDate(paginationDate, selectWorkSubmitInfoVos));
//    }

//    @Override
//    public Result addWorkInfoAnswer(WorkSubmitBo workSubmitBo,SelectWorkSubmitInfoBo selectWorkSubmitInfoBo) {
//
//        workSubmitBo.setCompleteTime(workSubmitBo.getCompleteTime()+":00");
//        selectWorkSubmitInfoBo.setCompleteDate(selectWorkSubmitInfoBo.getCompleteDate()+":00");
//        DataCheckUtils.isValid(DateUtils.compare(DateUtils.getCurrentTime(),workSubmitBo.getCompleteTime()+" "+selectWorkSubmitInfoBo.getCompleteDate()), "作业提交日期不能超前当前日期!");
//        DataCheckUtils.isValid(DateUtils.compare(workSubmitBo.getCompleteTime()+" "+selectWorkSubmitInfoBo.getCompleteDate(),workSubmitBo.getCompleteTime()+" "+selectWorkSubmitInfoBo.getCompleteDate()), "作业已停止提交！");
//        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getRemark(), "作业不能为空!");
//        DataCheckUtils.isNotBlank(selectWorkSubmitInfoBo.getAppendsLink(), "附件不能为空!");
//        LocalDateTime returnTime = parameter.getCompletionDateTime();
//        int isLeader = parameter.getIsleader();
//        System.out.println(returnTime);
//        System.out.println(isLeader);
//        try {
//            time1 = format.parse(workSubmitBo.getCreateTime());
//            time2 = format.parse(workSubmitBo.getCompleteTime());
//            long ss = (time2.getTime() - time1.getTime());
//            System.out.println(ss);
//            if (ss > 0) {
//                return Result.error("作业提交时间截至");
//            }
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        if (workSubmitBo.getRemark() == null) {
//            return Result.error("提交作业失败,作业内容不能为空");
//        } else {


//
//        Integer addWorkInfoAnswer = workSubmitInfoMapper.addWorkInfoAnswer(workSubmitBo.getWorkId(), workSubmitBo.getRemark(), workSubmitBo.getId(), workSubmitBo.getWorkStatus(), workSubmitBo.getCompleteTime());
//        return addWorkInfoAnswer < 1 ? Result.error("提交作业失败") : Result.success("提交作业成功");
//    }
//
//
//    @Override
//    public Result tWorkInfoAnswer(SelectWorkSubmitInfoBo selectWorkSubmitInfoBo) {
//        Integer tsAnswer = workSubmitInfoMapper.tWorkInfoAnswer(selectWorkSubmitInfoBo);
//        return tsAnswer < 1 ? Result.error("暂存作业失败") : Result.success("暂存作业成功");
//    }
//
//
//    @Override
//    public Result showWorkInfoAnswer(WorkSubmitBo workSubmitBo) {
//        WorkSubmitBo showHomeworkAnswer = workSubmitInfoMapper.showHomeworkAnswerStudent(workSubmitBo.getWorkId());
//        if (showHomeworkAnswer == null) {
//            return Result.error("作业已提交，你无法查看答案");
//        }
//        return Result.success(showHomeworkAnswer);
//    }
//
//    @Override
//    public Result showWorkInfoScoreStudent(WorkSubmitBo workSubmitBo) {
//        WorkSubmitBo homeworkScoreStudent = workSubmitInfoMapper.showHomeworkScoreStudent(workSubmitBo.getWorkId());
//        if (homeworkScoreStudent == null) {
//            return Result.success("还未批复");
//        } else {
//            return Result.success(homeworkScoreStudent);
//        }
//    }
}
